<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org" >
<head>
    <th:block th:include="include :: header('设错误码')" />
</head>
<body class="white-bg">
<div class="wrapper wrapper-content animated fadeInRight ibox-content">
    <input name="errorCodeSet" type="hidden"/>
    <form class="form-horizontal m" id="form-errorCode-update">
        <input name="branchEndId" type="hidden" th:value="${entityId}"/>
        <div class="form-group">
            <label class="col-sm-3 control-label is-required">错误码分类ID：</label>
            <div class="col-sm-8">
                <div class="input-group">
                    <input id="treeCode" type="hidden"/>
                    <input class="form-control" type="text"
                           onclick="selectErrorCodeCategoryTree()" id="treeName" readonly="true" required>
                    <span class="input-group-addon"><i class="fa fa-search"></i></span>
                </div>
            </div>
        </div>
        <div class="form-group">
            <label class="col-sm-3 control-label is-required">错误码：</label>
            <div class="col-sm-8" id="errorCode">
                <select class="form-control" name="errorCode">
                    <option value="0">--请选择--</option>
                </select>
            </div>
        </div>
    </form>
</div>
<th:block th:include="include :: footer" />
<script id="selectTpl" type="text/html">
    <select class="form-control errorCodeValue" name="errorCode">
        <option value="0">--请选择--</option>
        {{# layui.each(d.data, function(index, a){ }}
        <option value="{{a.errorCode}}"
                {{#if(a.errorCode==d.selectId){}}selected{{# }}}>{{a.errorCode}}/{{a.errorCodeDetail}}</option>
        {{# }); }}
    </select>
</script>
<script th:inline="javascript">
    var prefix = ctx + "processBranchEnd/processBranchEnd"
    $("#form-errorCode-add").validate({
        focusCleanup: true
    });
    getErrorCode()
    function submitHandler() {
        if ($.validate.form()) {
            if ($.validate.form()) {
                $.modal.close();
                // 父页面更新
                parent.editBranchEndErrorCode($('#form-errorCode-update').serialize());
            }
        }
    }
    /**
     *根据结束Id获取错误码信息
     */
    function getErrorCode() {
        let config = {
            url: prefix + "/get",
            type: "get",
            dataType: "json",
            data: {
                "branchEndId":  $("input[name='branchEndId']").val()
            },
            beforeSend: function () {
                $.modal.loading("正在处理中，请稍后...");
            },
            success: function (result) {
                $.modal.closeLoading();
                if (result.code == web_status.SUCCESS) {
                    $("input[name='errorCodeSet']").val(result.data.errorCode);
                    if(result.data.errorCodeCategoryCode){
                        $("#treeName").val(result.data.errorCodeCategoryName);
                        $("#treeCode").val(result.data.errorCodeCategoryCode);
                        queryErrorCode()
                    }
                    return false;
                }
                if (result.code == web_status.WARNING) {
                    $.modal.alertWarning(result.msg)
                    return false;
                }
                $.modal.alertError(result.msg);
            }
        };
        $.ajax(config);
    }
    /**
     * 根据errorCodeCategoryCode获取错误码列表
     */
    function queryErrorCode() {
        let config = {
            url: ctx + "errorCode/errorCode/queryErrorCode",
            type: "get",
            dataType: "json",
            data: {
                "errorCodeCategoryCode":  $("#treeCode").val()
            },
            beforeSend: function () {
                $.modal.loading("正在处理中，请稍后...");
            },
            success: function (result) {
                $.modal.closeLoading();
                if (result.code == web_status.SUCCESS) {
                    var selectTplHtml = $("#selectTpl").html();
                    result.selectId= $("input[name='errorCodeSet']").val()||''
                    laytpl(selectTplHtml).render(result, function (html) {
                        $("#errorCode").empty();
                        $("#errorCode").append(html);
                    });
                    return false;
                }
                if (result.code == web_status.WARNING) {
                    $.modal.alertWarning(result.msg)
                    return false;
                }
                $.modal.alertError(result.msg);
            }
        };
        $.ajax(config);
    }

    /*错误码分类-新增-选择父错误码分类树*/
    function selectErrorCodeCategoryTree() {
        var options = {
            title: '错误码分类选择',
            width: "380",
            url:  ctx + "errorCodeCategory/errorCodeCategory" + "/selectErrorCodeCategoryTree/",
            callBack: doSubmit
        };
        $.modal.openOptions(options);
    }

    function doSubmit(index, layero){
        var body = layer.getChildFrame('body', index);
        $("#treeCode").val(body.find('#treeCode').val());
        $("#treeName").val(body.find('#treeName').val());
        layer.close(index);
        queryErrorCode();
    }
</script>
</body>
</html>